package com.jushu.storbox.view.viewImpl;

import android.content.Context;
import android.util.AttributeSet;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.TranslateAnimation;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.handmark.pulltorefresh.library.PullToRefreshListView;
import com.jushu.storbox.R;


public class StyleView extends FrameLayout {
	
	private static final int SCROLL_DURATION = 300;
	private final static int LEFT_CLICKED = 0;
	private final static int RIGHT_CLICKED = 1;
	
	private LayoutParams ViewParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);

	private RelativeLayout rlLeftContainer = null;
	private TextView tvLeftPromit = null;
	private RelativeLayout rlRightContainer = null;
	private TextView tvRightPromit = null;
	private PullToRefreshListView dataListView = null;
	private View imgBackColor = null;
	private int currentIndex = 0;
	private int viewWidth = 0;
	
	OnSelectedListener selecteListener = null;
	
	public StyleView(Context context) {
		super(context);
		initView(context);
	}

	public StyleView(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
		initView(context);
	}

	public StyleView(Context context, AttributeSet attrs) {
		super(context, attrs);
		initView(context);
	}

	private void initView(Context context) {
		View view = LayoutInflater.from(context).inflate(R.layout.action_style_view, null, false);
		rlRightContainer = (RelativeLayout) view.findViewById(R.id.action_right_rl);
		tvLeftPromit = (TextView) view.findViewById(R.id.left_promit_text);
		rlLeftContainer = (RelativeLayout) view.findViewById(R.id.action_left_rl);
		tvRightPromit = (TextView) view.findViewById(R.id.right_promit_text);
		imgBackColor = view.findViewById(R.id.scroll_backgroud);
		dataListView = (PullToRefreshListView) view.findViewById(R.id.action_data_list);
		addView(view, ViewParams);
	}
	
	@Override
	public void computeScroll() {
		viewWidth = getWidth()/2;
		Log.i("width",viewWidth+"");
		ViewGroup.LayoutParams param = rlRightContainer.getLayoutParams();
		int old = param.width + rlRightContainer.getPaddingLeft() + rlRightContainer.getPaddingRight();
		param = imgBackColor.getLayoutParams();
		param.width = old;
		imgBackColor.setLayoutParams(param);
//		imgBackColor.
//		scroll_backgroud_line
		imgBackColor.setScrollX(0);
		super.computeScroll();
	}

	@Override
	protected void onFinishInflate() {
		tvLeftPromit.setTextColor(getResources().getColor(R.color.color_1a75c1));
		tvRightPromit.setTextColor(getResources().getColor(R.color.color_989898));
		
		rlRightContainer.setOnClickListener( new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				tvRightPromit.setTextColor(getResources().getColor(R.color.color_1a75c1));
				tvLeftPromit.setTextColor(getResources().getColor(R.color.color_989898));
				if(currentIndex != RIGHT_CLICKED) {
					TranslateAnimation animation = new TranslateAnimation(0, viewWidth, 0, 0);
					animation.setFillAfter(true);
					animation.setDuration(SCROLL_DURATION);
					imgBackColor.startAnimation(animation);
				}
				currentIndex = RIGHT_CLICKED;
				if(selecteListener != null) {
					selecteListener.onRightItemSelected(rlRightContainer);
				}
			}
		});
		
		rlLeftContainer.setOnClickListener( new OnClickListener() {
			@Override
			public void onClick(View arg0) {
				tvLeftPromit.setTextColor(getResources().getColor(R.color.color_1a75c1));
				tvRightPromit.setTextColor(getResources().getColor(R.color.color_989898));
				if(currentIndex != LEFT_CLICKED) {
					TranslateAnimation animation = new TranslateAnimation(viewWidth, 0, 0, 0);
					animation.setFillAfter(true);
					animation.setDuration(SCROLL_DURATION);
					imgBackColor.startAnimation(animation);
				}
				currentIndex = LEFT_CLICKED;
				if(selecteListener != null) {
					selecteListener.onLeftItemSelected(rlRightContainer);
				}
			}
		});
		
		super.onFinishInflate();
	}

	public void setOnSelectedListener(OnSelectedListener selecteListener) {
		this.selecteListener = selecteListener;;
	}
	
	public void setAdapter(ListAdapter listAdapter) {
		dataListView.setAdapter(listAdapter);
	}
	
	public PullToRefreshListView getListView() {
		return dataListView;
	}

	public TextView getLeftTextView() {
		return tvLeftPromit;
	}
	
	public TextView getRightTextView() {
		return tvRightPromit;
	}

	public void setLeftText(String leftText) {
		tvLeftPromit.setText(leftText);
	}
	
	public void setRightText(String rightText) {
		tvRightPromit.setText(rightText);
	}
	
	public interface OnSelectedListener {
		public void onLeftItemSelected(View view);

		public void onRightItemSelected(View view);
	}
}
